package com.imay.live.fragment.room;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.base.util.FileUtil;
import com.imay.live.Constants;
import com.imay.live.activity.dynamics.ReleaseActivity;
import com.imay.live.helper.FileHelper;
import com.imay.live.media.LivePlayer;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;
import com.tencent.rtmp.ITXLivePlayListener;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.rtmp.ui.TXCloudVideoView;

/**
 * Created by jeaman on 2017/5/9.
 */

public class LiveRecordPreviewFragment extends BaseDialogFragment implements View.OnClickListener, ITXLivePlayListener {
    private final static String BUNDLE_NAME_VIDEO_PATH = "video";
    private final static String BUNDLE_NAME_COVER_PATH = "cover";

    private TXCloudVideoView mVideoView;

    private LivePlayer mLivePlayer;
    private String mVideoPath;
    private String mCoverPath;
    private boolean mPublish;   //是否发布，发布则不删除源文件

    public static LiveRecordPreviewFragment newInstance(String videoPath, String coverPath) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_NAME_VIDEO_PATH, videoPath);
        bundle.putString(BUNDLE_NAME_COVER_PATH, coverPath);
        LiveRecordPreviewFragment fragment = new LiveRecordPreviewFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_live_record_preview;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mVideoPath = getArguments().getString(BUNDLE_NAME_VIDEO_PATH);
        mCoverPath = getArguments().getString(BUNDLE_NAME_COVER_PATH);

        mVideoView = $(R.id.live_record_preview_vv_video);

        $(R.id.live_record_preview_iv_close).setOnClickListener(this);
        $(R.id.live_record_preview_btn_save).setOnClickListener(this);
        $(R.id.live_record_preview_btn_share).setOnClickListener(this);

        mLivePlayer = new LivePlayer(getActivity(), mVideoView);
        startPlay();
    }

    @Override
    protected int getWindowAnimations() {
        return R.style.dialog_from_bottom;
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }

    private void startPlay() {
        mLivePlayer.setLocalUrl(mVideoPath);
        mLivePlayer.startPlay(this);
        mLivePlayer.setMute(true);
    }

    private void saveVideo() {
        if (FileUtil.moveFile(mVideoPath, FileHelper.getInstance().getImageFile().getAbsolutePath())) {
            dismiss();
            ToastUtil.show(getActivity(), R.string.live_record_save_success);
        } else {
            ToastUtil.show(getActivity(), R.string.live_record_save_failed);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mLivePlayer.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mLivePlayer.resume();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        mLivePlayer.onDestroy();
        if (!mPublish) {
            FileUtil.deleteFile(mVideoPath);
            FileUtil.deleteFile(mCoverPath);
        }
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.live_record_preview_iv_close: //关闭
                dismiss();
                break;
            case R.id.live_record_preview_btn_save: //保存
                saveVideo();
                break;
            case R.id.live_record_preview_btn_share:    //发布
                mPublish = true;
                ReleaseActivity.startActivity(getActivity(), mCoverPath, mVideoPath, null, ReleaseActivity.MOVIE, Constants.DYNAMICSFEED_REQUEST_CODE);
                dismiss();
                break;
        }
    }

    @Override
    public void onPlayEvent(int event, Bundle bundle) {
        if (event == TXLiveConstants.PLAY_EVT_PLAY_END) {   //循环播放
            startPlay();
        }

    }

    @Override
    public void onNetStatus(Bundle bundle) {

    }
}
